package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import com.wn.common.pojo.*;
import com.wn.mapper.SchemeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class SchemeService {

    @Autowired
    private SchemeMapper schemeMapper;

    /**
     * 获取所有的方案列表
     * @param
     * @return
     */
    public PageInfo<Scheme> getSchemeList(PageParam<Scheme> schemePageParam) {

            if (!StringUtils.isEmpty(schemePageParam.getSuperSearchKeyWord())) {

                //分页
                PageHelper.startPage(schemePageParam.getPageNum(), schemePageParam.getPageSize());
                //执行高级检索
                List<Scheme> schemeList = schemeMapper.superSearch(schemePageParam.getSuperSearchKeyWord());
                PageInfo<Scheme> schemePageInfo = new PageInfo<>(schemeList);
                return schemePageInfo;
            } else {
                //分页
                PageHelper.startPage(schemePageParam.getPageNum(), schemePageParam.getPageSize());
                List<Scheme> schemeList = schemeMapper.getSchemeList();
                PageInfo<Scheme> pageInfo = new PageInfo<>(schemeList);
                return pageInfo;
            }


        }

        /**
         * 添加一条方案表数据
         * @param scheme
         */
        public Rsp insertScheme(Scheme scheme) {
            //获取当前时间
            Date createdate = new Date();
            //转换时间格式
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //设置添加事件
            scheme.setCreateTime(Timestamp.valueOf(simpleDateFormat.format(createdate)));
            return schemeMapper.insertSelective(scheme) == 1? new Rsp().success(1): new Rsp().fail();

        }

        /**
         * 方案修改方法,根据id修改
         * @param scheme
         * @return
         */
        public Rsp updataScheme(Scheme scheme) {
            //获取当前时间
            Date updatedate = new Date();
            //转换时间格式
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //设置更新时间
            scheme.setUpdateTime(Timestamp.valueOf(simpleDateFormat.format(updatedate)));
            return schemeMapper.updateByPrimaryKeySelective(scheme) == 1? new Rsp().success(1): new Rsp().fail();
        }

        /**
         * 根据id删除一条数据
         * @param
         * @return
         */
        public Rsp deleteSchemeById(Scheme scheme) {
            int primaryKey = schemeMapper.deleteByPrimaryKey(scheme);
            if(primaryKey!=0){
                return new Rsp().success(primaryKey);
            }else{
                return new Rsp().fail().msg("删除失败");
            }
    }


    //修改方案的状态
    public Rsp updateSchemeStatus(Scheme scheme){
        if(scheme.getStatus().equals("1")){
            scheme.setStatus("0");
        }else {
            scheme.setStatus("1");
        }
        Integer updateSchemeStatus = schemeMapper.updateSchemeStatus(scheme);
        if(updateSchemeStatus.equals(null)){
            return new Rsp().fail().msg("修改失败！");
        }else{
            return new Rsp().success(updateSchemeStatus);
        }
    }

    //获取所有的方案列表无分页

    public Rsp getAllScheme() {
        List<Scheme> schemeList = schemeMapper.getSchemeList();
        return Rsp.success(schemeList);
    }


    public Rsp getSchemeItemByid(Integer id){
        Scheme schemeItemByid = schemeMapper.getSchemeItemByid(id);
        if(StringUtils.isEmpty(schemeItemByid)){
           return new Rsp().fail().msg("查询失败");
        }else {
            return new Rsp().success(schemeItemByid);
        }
    }
}
